var express = require('express')
var router = express.Router()

var fs = require('fs');
var path = require('path');
var multer = require('multer')

var upload = multer({ dest: 'tem_uploads/' })
    // 第三方config获取项目配置文件
var upload_config = require('config').get('upload_config')
    // console.log(upload_config);
router.post('/upload', upload.single('file'), (req, res, next) => {
    // res.ResData("文件上传")
    // console.log(req.file);
    var fileEXArray = req.file.originalname.split('.');
    var ext = fileEXArray[fileEXArray.length - 1]

    var targetPath = req.file.destination + req.file.filename + '.' + ext;
    // console.log(targetPAth);
    fs.rename(req.file.path, targetPath, (err) => {
        if (err) return res.ResData("上传失败")
    })
    res.ResData("上传成功", 200, { "tem_path": targetPath, 'url': upload_config.baseURL + targetPath })
})
module.exports = router